package com.rundatop.core.wxpay;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import org.jdom2.JDOMException;

import com.rundatop.core.wxpay.utils.XMLUtil;

public class WXJSPay {
	
	 public static void main(String[] args) throws Exception
     {
		 String out_trade_num=System.currentTimeMillis()+"";
		 String openid="oNWCiuCHiHRpKPec3NhlLodpTNa8";
		 Map<String, String> wxPayParamMap  = jsApiPay(openid, "1",out_trade_num);//测试金额为1分钱
			
     }

	 //小程序支付
	public static Map jsApiPay(String openid, String total_fee,
		String out_trade_num) throws JDOMException, IOException {	
		String noceStr=Sha1Util.getNonceStr();//随机字符串
		String timeStamp=Sha1Util.getTimeStamp();//时间戳
		// 接口package部分-内容----------------------------------------------------------
		TreeMap<String, String> contentMap = new TreeMap<String, String>();
		contentMap.put("appid", Constants.APPID); // 公众账号 ID
		contentMap.put("mch_id", Constants.MCH_ID); // 商户号
		contentMap.put("nonce_str", noceStr); // 随机字符串		
		contentMap.put("body", Constants.BODY); // 商品描述
		contentMap.put("out_trade_no",out_trade_num); // 商户订单号
		contentMap.put("total_fee",Tool.getMoney(total_fee)); // 订单总金额
		contentMap.put("spbill_create_ip",Tool.getip()); // 订单生成的机器IP
		contentMap.put("notify_url",Constants.NOTIFY_URL); // 通知地址
		contentMap.put("trade_type",Constants.TRADE_TYPE_JS); // 交易类型
		contentMap.put("openid",openid); // 用户标识
		String wxpackage = WeiXinSignAndPackage.createPackage(contentMap);
		//System.out.println("wxpackage:::::::::::::::"+wxpackage);
		contentMap.put("sign", wxpackage);
		String result=WeiXinSignAndPackage.getPrepayId(contentMap);//调用统一接口返回的值
		//System.out.println("result:::::::::::"+result);
		////System.out.println("result="+result);
		Map<String,String> map=XMLUtil.doXMLParse(result);//调用统一接口返回的值转换为XML格式
		//System.out.println(map);
		String return_code = (String) map.get("return_code");//返回状态码
		//System.out.println(map);
		TreeMap<String, String> wxPayParamMap = new TreeMap<String, String>();
        if(return_code.equals("SUCCESS")){  
			wxPayParamMap.put("appId", Constants.APPID);
			wxPayParamMap.put("timeStamp",timeStamp);		
			wxPayParamMap.put("nonceStr",noceStr);
			wxPayParamMap.put("package", "prepay_id="+map.get("prepay_id"));
			wxPayParamMap.put("signType", Constants.SIGNTYPE);
			String paySign=WeiXinSignAndPackage.createPaySign(wxPayParamMap);//支付得到的签名
			//System.out.println("paySign:::::::::::"+paySign);
			wxPayParamMap.put("paySign", paySign);
			wxPayParamMap.put("payMoney", total_fee);//到前段显示使用，支付不需要此参数 
        }
        return wxPayParamMap;
	}
	
	//返回前端--微信公众号支付所需要的参数
	public static Map<String,String> wxPayParameter(String openId,String total_fee,String orderId){
		Map<String, String> wxPayParamMap = null;
		try {			
			wxPayParamMap = WXJSPay.jsApiPay(openId, Tool.getMoney(total_fee), orderId);// 测试金额为1分钱
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	    Map <String, String> map = new HashMap<String, String>();
		map.put("appId", wxPayParamMap.get("appId"));
		map.put("timeStamp", wxPayParamMap.get("timeStamp"));
		map.put("nonceStr", wxPayParamMap.get("nonceStr"));
		map.put("wxPackage", wxPayParamMap.get("package"));
		map.put("signType", wxPayParamMap.get("signType"));
		map.put("paySign", wxPayParamMap.get("paySign"));		
		return map;
	}
}
